package com.watson.websocket.config1;


import org.springframework.context.event.EventListener;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionConnectedEvent;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;

/**
 * 监听类
 */
@Component
public class WebSocketEventListener {

    //监听连接
    @EventListener
    @Async
    public void handleConnectListener(SessionConnectedEvent event) {
        StompHeaderAccessor accessor = StompHeaderAccessor.wrap(event.getMessage());
        String sessionId = accessor.getSessionId();
        System.out.println("[ws-connect] socket connect："+sessionId);
    }

    //监听断开连接
    @Async
    @EventListener
    public void handleDisConnectListener(SessionDisconnectEvent event){
        StompHeaderAccessor accessor = StompHeaderAccessor.wrap(event.getMessage());
        String sessionId = accessor.getSessionId();
        System.out.println("[ws-disconnect] socket disconnect："+sessionId);
    }
}
